<template>
  <div>
    <!--引用表格-->
   <BasicTable @register="registerTable" :rowSelection="rowSelection">
     <!--插槽:table标题-->
      <template #tableTitle>
          <a-button type="primary" @click="handleAdd" preIcon="ant-design:plus-outlined"> 批量生成</a-button>
          <a-button type="primary" @click="handleSwap" preIcon="ant-design:plus-outlined"> 课日调整 </a-button>
          <a-button v-if="selectedRowKeys.length > 0" type="primary" @click="batchHandleDelete" preIcon="ant-design:plus-outlined"> 批量删除 </a-button>
          <!--<a-button  type="primary" preIcon="ant-design:export-outlined" @click="onExportXls"> 导出</a-button> -->
          
      </template>
      
       <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" :dropDownActions="getDropDownAction(record)"/>
      </template>
     
    </BasicTable>
    <!-- 表单区域 -->
    <CsSubjectOrderModal @register="registerModal" @success="handleSuccess"></CsSubjectOrderModal>
    <CsSubjectOrderSwapModal @register="registerSwapModal" @success="handleSuccess"></CsSubjectOrderSwapModal>
  </div>
</template>

<script lang="ts" name="cssubjectorder-csSubjectOrder" setup>
  import {reactive} from 'vue';
  import {BasicTable, TableAction} from '/@/components/Table';
  import {useModal} from '/@/components/Modal';
  import { useListPage } from '/@/hooks/system/useListPage'
  import CsSubjectOrderModal from './components/CsSubjectOrderModal.vue'
  import CsSubjectOrderSwapModal from './components/CsSubjectOrderSwapModal.vue'
  import {columns, searchFormSchema} from './CsSubjectOrder.data';
  import {list, deleteOne,batchDelete, getImportUrl,getExportUrl} from './CsSubjectOrder.api';
  const queryParam = reactive<any>({});
  //注册model
  const [registerModal, {openModal}] = useModal();
  const [registerSwapModal, {openModal:openSwapModal}] = useModal();
  
  //注册table数据
  const { tableContext } = useListPage({
      tableProps:{
           title: '科目订单表',
           api: list,
           columns,
           canResize:false,
           showActionColumn:false,
           clickToRowSelect:true,
           formConfig: {
              //labelWidth: 120,
              schemas: searchFormSchema,
              autoSubmitOnEnter:true,
              showAdvancedButton:true,
              fieldMapToNumber: [
              ],
              fieldMapToTime: [
                 ['attendDate', ['attendDate_begin', 'attendDate_end'], 'YYYY-MM-DD'],
              ],
            },
           actionColumn: {
               width: 120,
               fixed:'right'
            },
            beforeFetch: (params) => {
              return Object.assign(params, queryParam);
            },
      },
       exportConfig: {
            name:"科目订单表",
            url: getExportUrl,
            params: queryParam,
          },
          importConfig: {
            url: getImportUrl,
            success: handleSuccess
          },
  })

  const [registerTable, {reload},{ rowSelection, selectedRowKeys,selectedRows }] = tableContext


   /**
    * 新增事件
    */
  function handleAdd() {
     openModal(true, {
       isUpdate: false,
       showFooter: true,
     });
  }
  /**
    * 新增事件
    */
    function handleSwap() {
      let record = {personId:'',targetType:0,attendDate:''};
      if(selectedRows.value[0]) {
        let target = selectedRows.value[0];
        record = {
          personId:target.personId,
          targetType:0,
          attendDate:target.attendDate
        };
      }
     openSwapModal(true, {
       isUpdate: false,
       showFooter: true,
       record:record
     });
  }
   /**
    * 编辑事件
    */
  function handleEdit(record: Recordable) {
     openModal(true, {
       record,
       isUpdate: true,
       showFooter: true,
     });
   }
   /**
    * 详情
   */
  function handleDetail(record: Recordable) {
     openModal(true, {
       record,
       isUpdate: true,
       showFooter: false,
     });
   }
   /**
    * 删除事件
    */
  async function handleDelete(record) {
     await deleteOne({id: record.id}, handleSuccess);
   }
   /**
    * 批量删除事件
    */
  async function batchHandleDelete() {
     await batchDelete({ids: selectedRowKeys.value}, handleSuccess);
   }
   /**
    * 成功回调
    */
  function handleSuccess() {
      (selectedRowKeys.value = []) && reload();
   }
   /**
      * 操作栏
      */
  function getTableAction(record){
       return [
         {
           label: '编辑',
           onClick: handleEdit.bind(null, record),
         }
       ]
   }
     /**
        * 下拉操作栏
        */
  function getDropDownAction(record){
       return [
         {
           label: '详情',
           onClick: handleDetail.bind(null, record),
         }, {
           label: '删除',
           popConfirm: {
             title: '是否确认删除',
             confirm: handleDelete.bind(null, record),
             placement: 'topLeft',
           }
         }
       ]
   }


</script>

<style scoped>

</style>